﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StuMngVan.Entities;
using StuMngVan.Dals;

namespace StuMngVan
{
    public partial class FormStudent : Form
    {
        public FormStudent()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string sTxtName = txtName.Text;
            List<Student> students = Dal4Student.SearchAllStudents(sTxtName);
            gvStudents.DataSource = students;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FormAddStudent formAddStu = new FormAddStudent();
            formAddStu.ShowDialog();
            btnSearch.PerformClick();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            Student stuSelected = gvStudents.SelectedRows[0].DataBoundItem as Student;
            Dal4Student.DeleteStudent(stuSelected);
            MessageBox.Show(string.Format("删除学生{0}成功", stuSelected.Name));
            btnSearch.PerformClick();
        }

        private void btnUpdateAge_Click(object sender, EventArgs e)
        {
            Dal4Student.UpdateAge();
            MessageBox.Show(string.Format("恭喜你更新学生年龄成功！"));
            btnSearch.PerformClick();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            //if (gvStudents.SelectedRows[0].DataBoundItem == null)
            //    return;

            if (this.gvStudents.SelectedRows.Count == 0)
                MessageBox.Show("请选择要修改的行！", "提示");
            else
            {
                Student selectedStu = gvStudents.SelectedRows[0].DataBoundItem as Student;

                FormEditStudent formEdit = new FormEditStudent();
                ////【闻祖东 2013-3-28-193332】接下来是赋值的过程。
                formEdit.StuAddress = selectedStu.Address;
                formEdit.StuAge = selectedStu.Age;
                formEdit.StuName = selectedStu.Name;
                formEdit.StuRemark = selectedStu.Remark;

                formEdit.ShowDialog();
            }

            //Student selectedStu = gvStudents.SelectedRows[0].DataBoundItem as Student;

            //FormEditStudent formEdit = new FormEditStudent();
            //////【闻祖东 2013-3-28-193332】接下来是赋值的过程。
            //formEdit.StuAddress = selectedStu.Address;
            //formEdit.StuAge = selectedStu.Age;
            //formEdit.StuName = selectedStu.Name;
            //formEdit.StuRemark = selectedStu.Remark;

            //formEdit.ShowDialog();
        }
    }
}
